package by.bucha.testproject.service.handlers.impl;

import com.google.gson.Gson;

import by.bucha.testproject.content.bo.workers.LeaderModel;
import by.bucha.testproject.content.bo.workers.SimpleWorkerModel;
import by.bucha.testproject.content.contract.TaskContract;
import by.bucha.testproject.service.ApplicationService;
import by.bucha.testproject.service.handlers.BaseHandler;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.ResultReceiver;

public class DbQueryHandler extends BaseHandler {

	private static final String NO_DATA_TYPE_EXCEPTION_TEXT = "Cant find data type to return!";

	private static final String BAD_URI_EXCEPTION_TEXT = "Bad uri!";

	@Override
	public void doExecute(final Intent intent, final Context context,
			final ResultReceiver callback) {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Uri uri = Uri.parse(intent
						.getStringExtra(ApplicationService.EXTRA_URI));
				String selection = intent
						.getStringExtra(ApplicationService.EXTRA_SELECTION);
				String[] selectionArgs = intent
						.getStringArrayExtra(ApplicationService.EXTRA_SELECTION_ARGS);
				String[] columns = intent
						.getStringArrayExtra(ApplicationService.EXTRA_COLUMNS);
				String sortOrder = intent
						.getStringExtra(ApplicationService.EXTRA_SORT_ORDER);
				Cursor cursor = null;

				String dataToReturn;
				String dataType = "";

				if (uri.toString().contains(
						TaskContract.WorkerColumns.CONTENT_URI.toString())) {

					cursor = context.getContentResolver().query(uri, columns,
							selection, selectionArgs, sortOrder);

					if (cursor != null && cursor.getCount() != 0) {
						cursor.moveToPosition(0);

						dataType = cursor.getString(cursor
								.getColumnIndex(TaskContract.WorkerColumns.WORKER_TYPE));

						if (dataType
								.equals(TaskContract.WorkerColumns.LEADER_TABLE_TYPE)) {

							LeaderModel model = fromCursorToLeaderModel(cursor);
							dataToReturn = new Gson().toJson(model);

						} else if (dataType
								.equals(TaskContract.WorkerColumns.SIMPLE_WORKER_TABLE_TYPE)) {

							SimpleWorkerModel model = fromCursorToSimpleWorkerModel(cursor);
							dataToReturn = new Gson().toJson(model);

						} else {
							cursor.close();
							throw new NullPointerException(
									NO_DATA_TYPE_EXCEPTION_TEXT);
						}
						cursor.close();
					} else {
						dataToReturn = null;
					}
				} else {
					throw new NullPointerException(BAD_URI_EXCEPTION_TEXT);
				}
				Bundle bundle = new Bundle();

				bundle.putString(TaskContract.WorkerColumns.WORKER_TYPE,
						dataType);
				bundle.putString(ApplicationService.EXTRA_QUERY_RESPONSE,
						dataToReturn);

				sendUpdate(SUCCESS_RESPONSE, bundle);
			}
		}).start();
	}

	private LeaderModel fromCursorToLeaderModel(Cursor cursor) {

		LeaderModel model = new LeaderModel();

		model.setEndTimeReception(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.END_TIME_RECEPTION)));

		model.setId(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.WORKER_ID)));

		model.setName(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.NAME)));

		model.setPatronymic(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.PATRONYMIC)));

		model.setSalary(cursor.getInt(cursor
				.getColumnIndex(TaskContract.WorkerColumns.SALARY)));

		model.setStartTimeReception(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.START_TIME_RECEPTION)));

		model.setSurname(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.SURNAME)));
		return model;
	}

	private SimpleWorkerModel fromCursorToSimpleWorkerModel(Cursor cursor) {

		SimpleWorkerModel model = new SimpleWorkerModel();

		model.setLaunchTime(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.LUNCH_TIME)));

		model.setId(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.WORKER_ID)));

		model.setName(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.NAME)));

		model.setPatronymic(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.PATRONYMIC)));

		model.setSalary(cursor.getInt(cursor
				.getColumnIndex(TaskContract.WorkerColumns.SALARY)));

		model.setWorkplaceNumber(cursor.getInt(cursor
				.getColumnIndex(TaskContract.WorkerColumns.WORKPLACE_NUMBER)));

		model.setSurname(cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.SURNAME)));
		return model;
	}
}
